<template>
    <el-dialog v-model="dialogVisible" title="商品选择" width="80%">
        <el-table ref="mulitipTableRef" @selection-change="handleSelectionChange" :data="tableData" stripe
            style="width: 100%" v-loading="loading" height="300px" destroy-on-close>
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="商品">
                <template #default="{ row }">
                    <div class="noticeGoods">
                        <el-image class="noticeGoodsImage" :src="row.cover" fit="cover" :lazy="true"
                            style="width: 50px; height: 50px;"></el-image>
                        <div class="noticeGoodsDiv">
                            <p>{{ row.title }}</p>
                            <p class="noticeGoodsP1">分类：{{ row.category ? row.category.name : "未分类" }}</p>
                            <p class="noticeGoodsP2">创建时间：{{ row.create_time }}</p>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="总库存" width="90" prop="stock" align="center">
            </el-table-column>
            <el-table-column label="价格（元）" width="150" align="center">
                <template #default="{ row }">
                    <span class="chooseGoodsPriceTextTop">￥{{ row.min_price }}</span>
                    <el-divider direction="vertical" />
                    <span class="chooseGoodsPriceTextBottom">￥{{ row.min_oprice }}</span>
                </template>
            </el-table-column>
        </el-table>
        <div class="noticeBottom">
            <el-pagination background layout="prev,pager, next" :total="total" :current-page="currentPage"
                :page-size="limit" @current-change="getData" />
        </div>
        <template #footer>
            <span>
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="submit">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup>
import { ref } from 'vue'
import { useInitTable } from '@/hooks/useCommon'
import { getGoodsList } from '@/api/goods'

const {
    mulitipTableRef,
    handleSelectionChange,
    handleMultiDelete,
    searchForm,
    resetSearchForm,
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
    handleDelete,
    handleStatusChange,
    handleMultiStatusChange,
    multiSelectionId
} = useInitTable({
    getList: getGoodsList,
    onGetListSuccess: (res) => {
        tableData.value = res.list
        total.value = res.totalCount
    },
})

const dialogVisible = ref(false)

const close = () => {
    dialogVisible.value = false
}

const callbackFunction = ref(null)
const open = (callback = null) => {
    callbackFunction.value = callback
    dialogVisible.value = true
}

const submit = () => {
    if (typeof callbackFunction.value == 'function') {
        callbackFunction.value(multiSelectionId.value)
    }
    close()
}

defineExpose({
    open
})
</script>
<style scoped lang="less">
.chooseGoodsPriceTextTop {
    color: red
}

.chooseGoodsPriceTextBottom {
    color: #747474;
    font-size: 14px;
}

.noticeBottom {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 10px;
}
</style>